<?php

namespace Admin\Controller;

/**
 * 会员类
 *
 * Class MemberController
 * @package Admin\Controller
 */
class MemberController extends AdminCommonController
{
    /**
     * 会员列表
     */
    public function index()
    {
        $model = M('member');
        $order = array(
            'regtime' => 'desc'
        );
        $list = $model->where(array('del_flg' => 0))->order($order)->select();

                //$list['match_flg'] = C('ddz_match_flg')[$list['match_flg']];

            $this->assign('member_info', $list);

        $this->display();
    }

    /**
     * 会员详细
     */
    public function detail()
    {
        $id = I('id');
        $model = M('member');
        $list = $model->getById($id);

                $list['match_flg'] = C('ddz_match_flg')[$list['match_flg']];

            $this->assign('vo' ,$list);

            $this->display();
    }

    /**
     * 停赛
     */
    public function memberStatus()
    {
        $id     = I('id');
        $status = I('status');

        $model = M('member');
        $res = $model->where(array('id' => $id))->save(array('match_flg' => $status));
        //var_dump($res);exit;
        //echo $model->getLastSql();exit;
        if ($res!==false) {
            $status = 100;
            $message = '操作成功';
        } else {
            $status = 101;
            $message = '操作失败';
        }
        $this->showMessage(array(), $status, $message);
    }

    /**
     * 账户详细
     */
    public function account()
    {
        $id = I('id');
        $list = M('account')->where(array('member_id' => $id))->select();
        if ($list) {
            $this->assign('account', $list);
        }
        $this->display();
    }

    /**
     * 信息补充
     */
    public function supplement()
    {
        $id = I('id');
        $cityModel = M('city');
        $info = M('member')->where(array('id' =>$id))->find();
        // 头像
        if (!empty($info['photo'])) {
            $data['photo'] = $info['photo'];
        }

        // 省市区联动
        $cityList1 = M('city')->where(array('level' => 1))->select();
        if (!empty($info['live_id'])) {
            $cityData2 = $cityModel->where(array('id' => $info['live_id']))->find();
            // 直辖市处理
            if ($cityData2['pid'] === '0') {
                $cityData1 = $cityData2['id'];
            } else {
                $cityData1 = $cityData2['pid'];
            }
            $cityList2 = $cityModel->where(array('pid' => $cityData2['pid']))->select();
            $this->assign('citylist2' ,$cityList2)
                 ->assign('citydata2' ,$cityData2)
                 ->assign('citydata1', $cityData1);
        }
        // 性别
        if (!empty($info['sex'])) {
            $data['sex'] = $info['sex'];
        }
        // 个人简介
        if (!empty($info['content_id'])) {
            $content = M('contentData')->where(array('id' => $info['content_id']))->getField('content');
            // 去掉斜杠
            $content = contentShow($content);

            $this->assign('content_data', $content);
        }
        $this->assign('data', $data)
             ->assign('citylist1', $cityList1)
             ->assign('province', $cityList1)
             ->assign('id', $id)
             ->display();
    }

    /**
     * 信息补充处理
     */
    public function doSupplement()
    {
        $fileData = $_FILES;

        $member_id = I('member_id');
        $content   = I('content');
        $live_id   = I('live_id');
        $sex       = I('sex');

        // 1 富文本编辑器处理
        $content = contentFilter($content);
        $content_id = M('contentData')->add(array('content' => $content));

        $data2 = array(
            'sex'        => $sex,
            'live_id'    => $live_id,
        );

        // 2 上传图片
        if (!empty($fileData['photo']['name'])) {
            $file_path = M('member')->where(array('id' => $member_id))->getField('photo');
            if (empty($file_path)) {
                $upload_path = uploadImage($member_id, 'photo', $fileData, 'mem_' ,null, false);
            } else {
                $upload_path = uploadImage($member_id, 'photo', $fileData, 'mem_', $file_path);
            }
            if ($upload_path) {
                $data2['photo'] = $upload_path;
            }
        }
        if ($content_id) {
            $data2['content_id'] = $content_id;
        }

        // 3 更新用户信息
        $res = M('member')->where(array('id' => $member_id))->save($data2);
        if ($res) {
            $this->redirect('/member/index');
        } else {
            $this->redirect('/public/error-500');
        }
    }
}